import axios from 'axios';
import { urls } from './url.js';
import { baseUrl } from '@/utils/env';

const upload = (formData, config) => {
  let { type = 'avatar', userId = '' } = config;
  let headers = {
    'Content-Type': 'multipart/form-data',
  };
  if (type == 'avatar') {
    headers.action = 'avatar';
    headers.TUID = userId;
  }
  console.log('has upload start: formData', formData);
  return new Promise((resolve, reject) => {
    axios
      .post(baseUrl + 'upload', formData, {
        headers,
      })
      .then((res) => {
        if (res.data.code !== 200) {
          console.log('upload err', res.data || res);
          reject(res.data || res);
        }
        resolve(res.data.data);
      })
      .catch((err) => {
        console.log('upload err', err.data || err);
        reject(err.data || err);
      });
  });
};

export { upload };
